home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / CLOCK.CPP < prev    next >
C/C++ Source or Header  |  1994-08-04  |  2KB  |  114 lines

  1. // Copyright 1993 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include "clock.h"
  4. #include "display.h"
  5. #include "globals.h"
  6. #include "msgs.h"
  7.  
  8. Clock::Clock( HWND parent )
  9. : pWin(parent), running(True), side_to_move(White), dir(Up)
  10. {
  11.     limit[White] = limit[Black] = 0;
  12.     reset();
  13. }
  14.  
  15. Clock::~Clock()
  16. {
  17. }
  18.  
  19. void Clock::reset()
  20. {
  21.      etime[White] = etime[Black] = 0;
  22.      last_start[White] = time(NULL);
  23.      show_time(White);
  24.      show_time(Black);
  25.      was_reset = True;
  26.      running = True;
  27.      stopped = False;
  28.      time_up = False;
  29. }
  30.  
  31. void Clock::start( const ColorType side )
  32. {
  33.      const ColorType oside = OppositeColor(side);
  34.      if (!was_reset)
  35.      {
  36.         etime[oside] += time(NULL) - last_start[oside];
  37.         show_time(oside);
  38.      }
  39.      was_reset = False;
  40.      last_start[side] = time(NULL);
  41.      side_to_move = side;
  42.      running = True;
  43.      stopped = False;
  44. }
  45.  
  46. void Clock::pause()
  47. {
  48.      running = False;
  49. }
  50.  
  51. void Clock::resume()
  52. {
  53.      running = True;
  54. }
  55.  
  56. void Clock::stop()
  57. {
  58.      running = False;
  59.      stopped = True;
  60. }
  61.  
  62. // This is only really meaningful when "counting down":
  63. time_t Clock::time_left( const ColorType side )
  64. {
  65.      time_t elapsed = elapsed_time(side) + time(NULL) - 
  66.     last_start[side];
  67.      elapsed = (limit[side] > elapsed) ? 
  68.         limit[side] - elapsed : 0;
  69.      return elapsed;
  70. }
  71.  
  72.  
  73. void Clock::update()
  74. {
  75.    if (running)
  76.    {
  77.        time_t elapsed = elapsed_time(side_to_move) + time(NULL) - 
  78.         last_start[side_to_move];
  79.        time_t display_time = elapsed;    
  80.        if (dir == Down)
  81.           display_time = (limit[side_to_move] > elapsed) ? 
  82.                     limit[side_to_move] - elapsed : 0;
  83.        Display::show_time(pWin, display_time, side_to_move);
  84.        if (dir == Down && display_time == 0 && !time_up)
  85.        {
  86.       time_up = True;
  87.           etime[side_to_move] = elapsed;
  88.       stop(); // prevent further updates
  89.           appWin->sendMsg(WM_COMMAND,MYIDM_LOSSONTIME,0L);
  90.        }
  91.    }
  92. }
  93.  
  94. void Clock::count_up()
  95. {
  96.     dir = Up;
  97. }
  98.  
  99. void Clock::count_down(time_t lim, const ColorType side)
  100. {
  101.     dir = Down; 
  102.     limit[side] = lim;
  103.     etime[side] = 0;
  104. }
  105.  
  106. void Clock::show_time( ColorType side )
  107. {
  108.     time_t elapsed = elapsed_time(side);
  109.     if (dir == Down)
  110.         elapsed = (limit[side] > elapsed) ? 
  111.                    limit[side] - elapsed : 0;
  112.     Display::show_time(pWin,elapsed,side);
  113. }
  114.